<template>
    <div class="box">
        <h1 style="text-align: center;">商品列表</h1>
        <br>
        <router-link to="/dish/add">
            <el-button type="primary" v-permission="{ path: '/dish/add', name: '商品添加' }">商品添加</el-button>
        </router-link>
        <el-table :data="dishList" height="250" border style="width: 100%">
            <el-table-column prop="name" label="商品名称" width="130"></el-table-column>
            <el-table-column prop="price" label="商品价格" width="80"></el-table-column>
            <el-table-column label="商品图片" width="130">
                <template slot-scope="scope">
                    <img width=100 height=100 :src="'/admin/' + scope.row.imgpath" alt="">
                </template>
            </el-table-column>
            <el-table-column label="商品规格" width="250">
                <template slot-scope="scope">
                    <span>{{ scope.row.specifications | spedAndKind }}</span>
                </template>
            </el-table-column>
            <el-table-column label="商品类别" width="100">
                <template slot-scope="scope">
                    <span>{{ scope.row.kind | spedAndKind }}</span>
                </template>
            </el-table-column>
            <el-table-column label="是否上架" width="80">
                <template slot-scope="scope">
                    <el-switch v-model="scope.row.enable" active-color="#13ce66" inactive-color="#ff4949"></el-switch>
                </template>
            </el-table-column>
            <el-table-column prop="catname" label="商品分类" width="80"></el-table-column>
            <el-table-column label="操作" v-permission="{ path: '/dish/edit', name: '商品修改' }">
                <template slot-scope="scope" width="150">
                    <el-button type="success"
                        @click="$router.push({ path: '/dish/edit', query: { id: scope.row._id } })" v-permission="{ path: '/dish/edit', name: '商品修改' }">编辑</el-button>
                    <el-button type="danger" @click="handleDelete(scope.$index, scope.row)" v-permission="{ path: '/dish/edit', name: '商品删除' }">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script>
import { dishDel, dishList } from '@/request'
import permission from '@/utils/permission'

export default {
    directives: {
        permission
    },
    data() {
        return {
            dishList: []
        }
    },
    methods: {
        async getDishList() {
            let data = await dishList()
            if (data.error_code === 200) {
                this.dishList = data.data.dishlist
            };
        },
        async handleDelete(index, row) {
            // console.log(index, row);
            this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                dishDel(row._id).then(res => {
                    let { msg, error_code } = res
                    if (error_code === 200) {
                        this.dishList.splice(index, 1)
                        this.$message({
                            type: 'success',
                            message: msg
                        });
                    } else {
                        this.$message({
                            type: 'error',
                            message: msg
                        });
                    }
                })
            })

        }
    },
    filters: {
        spedAndKind(value) {
            return value.split('=====').join('/')
        }
    },
    created() {
        this.getDishList()
    }
}
</script>

<style>
.el-table,
.box {
    height: 90% !important
}
</style>
